package com.buer.storage.service.impl;

import com.buer.storage.dao.CorderDao;

import com.buer.storage.dto.*;
import com.buer.storage.entity.*;
import com.buer.storage.service.CorderService;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CorderServiceImpl implements CorderService {
    private SnowFlake snowFlake=new SnowFlake(2,3);
    @Resource
    private CorderDao corderDao;

    //增加盘点所有信息
    @Transactional
    @Override
    public int insertCorder(CorderStorageDto corderStorageDto) {
        String orderNo=String.valueOf(snowFlake.nextId());
        corderStorageDto.setCheckCode("CS" + orderNo);
        int result = corderDao.insertCorder(corderStorageDto);
        for (int i = 0; i < corderStorageDto.getCorderPutProds().length; i++) {
            CorderdetailsDto corderPutProd = corderStorageDto.getCorderPutProds()[i];
            corderPutProd.setCorderCheckid(corderStorageDto.getCheckId());
        }
        corderDao.insertCorderdetails(corderStorageDto.getCorderPutProds());

        return result;
    }

    @Override
    public PageInfo<Corder> selectByAll(Map<String, Object> map) {

        PageHelper.startPage(map); //开始起始页
        List<Corder> list = corderDao.selectByAll(map); // 获取数据
        PageInfo<Corder> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    //根据盘点表id查询盘点信息
    @Override
    public List<CorderStorageDto> findCorderAllById (int checkId){
        List<CorderStorageDto> corderAll = corderDao.findCorderAllById(checkId);
        if (corderAll.size() > 0) {
            for (int i = 0; i < corderAll.size(); i++) {
                CorderdetailsDto[] all = corderDao.findCorderdetailsAllById(corderAll.get(i).getCheckId());
                if (all.length > 0) {
                    corderAll.get(i).setCorderPutProds(all);
                }
            }
        }

        return corderAll;
    }

    //查询所有盘点信息
    @Override
    public List<CorderDto> findCorderAll() {
        List<CorderDto> corderAll = corderDao.findCorderAll();
        return corderAll;
    }

    //根据盘点单ID删除盘点信息
    @Transactional
    @Override
    public int deleteCorderAllById(Integer checkId) {
        int deleteCorderAllById = corderDao.deleteCorderAllById(checkId);
        return deleteCorderAllById;
    }

    //修改盘点商品信息
    @Override
    public int updateCorderdetails(CorderStorageDto corderStorageDto) {
        Integer checkId = corderStorageDto.getCheckId();
        corderDao.deleteCorderByCheckId(checkId);
        corderDao.deleteCorderDetailsByCheckId(checkId);
        int corder = corderDao.insertCorder(corderStorageDto);
        for (int i = 0;i<corderStorageDto.getCorderPutProds().length;i++){
            CorderdetailsDto corderPutProd = corderStorageDto.getCorderPutProds()[i];
            corderPutProd.setCorderCheckid(corderStorageDto.getCheckId());
        }
        corderDao.insertCorderdetails(corderStorageDto.getCorderPutProds());
        return corder;
    }

    @Override
    public List<StockDto> findStockByWarehouseId(Integer warehouseId) {
        List<StockDto> stockByWarehouseId = corderDao.findStockByWarehouseId(warehouseId);
        return stockByWarehouseId;
    }

    @Override
    public List<StockDto> findStockByProdArea(Integer prodArea,Integer warehouseId) {
        List<StockDto> stockByProdArea = corderDao.findStockByProdArea(prodArea,warehouseId);
        return stockByProdArea;
    }

    @Override
    public List<StockDto> findStockByProdDepart(Integer prodDepart,Integer prodArea,Integer warehouseId) {
        List<StockDto> stockByProdDepart = corderDao.findStockByProdDepart(prodDepart,prodArea,warehouseId);
        return stockByProdDepart;
    }

    @Override
    public PageInfo<StockDto> searchStockAll(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<StockDto> list = corderDao.searchStockAll(map); // 获取数据
        PageInfo<StockDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<CorderDto> getCord(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<CorderDto> list = corderDao.getCord(map); // 获取数据
        PageInfo<CorderDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<Corderdetails> getCorderdetails(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Corderdetails> list = corderDao.getCorderdetails(map); // 获取数据
        PageInfo<Corderdetails> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public List<CorderDto> findCorderByStatus(Integer checkStatus) {
        List<CorderDto> corderByStatus = corderDao.findCorderByStatus(checkStatus);
        return corderByStatus;
    }

    @Override
    public List<Stockdetails> findStockdetailsByCheckid(Integer checkId) {
        List<Stockdetails> stockdetails = corderDao.findStockdetailsByCheckid(checkId);
        return stockdetails;
    }

    @Override
    public int findCountByStatus(Integer checkStatus) {
        int count = corderDao.findCountByStatus(checkStatus);
        return count;
    }

}
